package com.ruoyi.web.test;

import io.minio.*;
import io.minio.errors.MinioException;
import io.minio.http.Method;

public class FileUploader {
    public static void main(String[] args) throws Exception {

        //MinioClient minioClient = new MinioClient("http://192.168.188.10:9090","username","password");
        try {
            // 使用MinIO服务的URL，端口，Access key和Secret key创建一个MinioClient对象
            //MinioClient minioClient = new MinioClient("http://192.168.188.10:9090","username","password");
            MinioClient minioClient =
                    MinioClient.builder()
                            .endpoint("http://192.168.188.10:9000")
                            .credentials("username","password")
                            .build();

            // Make 'asiatrip' bucket if not exist.
            boolean found =
                    minioClient.bucketExists(BucketExistsArgs.builder().bucket("asiatrip").build());
            if (!found) {
                // Make a new bucket called 'asiatrip'.
                minioClient.makeBucket(MakeBucketArgs.builder().bucket("asiatrip").build());
            } else {
                System.out.println("Bucket 'asiatrip' already exists.");
            }

            // Upload '/home/user/Photos/asiaphotos.zip' as object name 'asiaphotos-2015.zip' to bucket
            // 'asiatrip'.
            minioClient.uploadObject(
                    UploadObjectArgs.builder()
                            .bucket("asiatrip")
                            .object("girl.png")
                            .filename("C:\\Users\\wcy\\Pictures\\Camera Roll\\1.png")
                            .build());
            System.out.println("美女图片已上传");

            String url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
                            .method(Method.GET)
                            .bucket("asiatrip")
                            .object("girl.png")
                    .build());
            System.out.println(url);

        } catch(MinioException e) {
            System.out.println("Error occurred: " + e);
        }
    }

}
